﻿CREATE PROCEDURE dbo.s_InsertPage
	@LoanID int,
	@ImageTypeID int,
	@PageNumber int,
	@Glossary text,
	@Tiff image = null,
	@MSWord image = null,
	@PDF image = null
AS

DECLARE @PageID int

PRINT 's_InsertPage 1 @LoanID:'+CONVERT(varchar,@LoanID)+',@ImageTypeID:'+CONVERT(varchar,@ImageTypeID)

IF NOT EXISTS(SELECT * FROM t_Image WHERE LoanID = @LoanID AND ImageTypeID = @ImageTypeID)
	INSERT INTO t_Image(LoanID,ImageTypeID) VALUES(@LoanID,@ImageTypeID)

PRINT 's_InsertPage 2'

INSERT INTO t_Page (ImageID,Glossary,PageNumber_)
SELECT ID,@Glossary,@PageNumber FROM t_Image
WHERE LoanID = @LoanID AND ImageTypeID = @ImageTypeID

PRINT 's_InsertPage 3'

SET @PageID = IDENT_CURRENT('t_Page')

PRINT 's_InsertPage(Tiff):Page '+convert(varchar,@PageID)
IF NOT @Tiff IS NULL
	INSERT INTO t_PageStore(PageID,PageTypeID,Data)
	VALUES(@PageID,0,@Tiff)

PRINT 's_InsertPage(Word):Page '+convert(varchar,@PageID)
IF NOT @MSWord IS NULL
	INSERT INTO t_PageStore(PageID,PageTypeID,Data)
	VALUES(@PageID,1,@MSWord)

PRINT 's_InsertPage(PDF):Page '+convert(varchar,@PageID)
IF NOT @PDF IS NULL
	INSERT INTO t_PageStore(PageID,PageTypeID,Data)
	VALUES(@PageID,2,@PDF)


